﻿using System;
using UnityEngine;

public class TimerMgr : Singleton<TimerMgr>
{
    /// <summary>
    /// 
    /// </summary>
   public event Action<float> _timerLoopCallBack;

    /// <summary>
    /// 创建定时器
    /// </summary>
    /// <param name="deltaTime">间隔时间</param>
    /// <param name="repeatTimes">重复次数 大于0 正常次数 小于0 无限循环</param>
    /// <param name="callBack">回调事件</param>
    /// <returns></returns>
    public Timer CreateTimer(float deltaTime,int repeatTimes,Action callBack,bool IsStart=true)
    {
        Timer timer = new Timer();
        timer.DeltaTime = deltaTime;
        timer.RepeatTimes = repeatTimes;
        timer.CallBack = callBack;

        if(IsStart)
        {
            timer.Start();
        }
        return timer;
    }

   

    /// <summary>
    /// 
    /// </summary>
    /// <param name="deltaTime">计时器驱动间隔</param>
    public void Loop(float deltaTime)
    {
        if(_timerLoopCallBack!=null)
        {
            _timerLoopCallBack(deltaTime);
        }
    }

}
[System.Serializable]
public class Timer
{
    public float DeltaTime;
    public int RepeatTimes;
    public Action CallBack;
    [HideInInspector]
    public bool IsRuning = false;
    public float duringTime { get; private set; }//计时持续时间
    //private int repeatedTimes;//已经执行了多少次
    public float repeatedTimes { get; private set; }//计时持续时间
    public bool isStart = false;//是不是一开始就要执行一次
    //public float DuringTime { get ;private set; }
    public void Start()
    {
        //Reset();
        IsRuning = true;
        TimerMgr.Instance._timerLoopCallBack += Loop;
    }

    public void Pause()
    {
        IsRuning = false;
        TimerMgr.Instance._timerLoopCallBack -= Loop;
    }

    public void Stop()
    {
        //Pause();
        Reset();
    }
    /// <summary>
    /// 重置已经进行的时间
    /// </summary>
    public void ResetTime(float newTime=0)
    {
        duringTime = newTime;

    }
    /// <summary>
    /// 重置时间上限
    /// </summary>
    public void ResetDeltaTime(float newTime = 0)
    {
        DeltaTime = newTime;
    }
    private void Reset()
    {
        duringTime = 0;
       
        repeatedTimes = 0;
        //RepeatTimes = 0;
        //CallBack = null;
        Pause();
    }

    public void Loop(float deltaTime)
    {
        if (!IsRuning || UnityEngine.Time.timeScale == 0) { return; }
        duringTime += deltaTime;
        //判断时间间隔
        if (duringTime > DeltaTime || Until.FloatEqual(duringTime, DeltaTime)||isStart)
        {
            ++repeatedTimes;
            duringTime -= DeltaTime;
            if (CallBack != null)
            {
                CallBack();
                isStart = false;
            }

            if (RepeatTimes >= 0 && repeatedTimes >= RepeatTimes)
            {
                Stop();
            }
        }
       


    }
}

//public class GameTimers
//{
//    private float _maxTime;
//    private float _time;
//    private Action _onFinish;

//    public bool IsFinished => _time >= _maxTime;
//    public bool IsRunning => _time < _maxTime;
//    public bool IsPause { get; private set; }
//    public float Time => _time;

//    public float MaxTime
//    {
//        get => _maxTime;
//        set => _maxTime = value;
//    }

//    public GameTimers(float maxTime)
//    {
//        if (maxTime <= 0)
//        {
//            throw new Exception($"_maxTime can not be 0 or negative");
//        }
//        _maxTime = maxTime;
//        _time = 0f;
//    }

//    public void Reset()
//    {
//        _time = 0f;
//    }
//    public void Reset(float max)
//    {
//        _time = 0f;
//        _maxTime = max;
//    }
//    public void Finish(float finish = 1)
//    {
//        _time = _maxTime * finish;
//        _time = Mathf.Clamp(_time, 0, _maxTime * 2);

//    }
//    public void OnPause()
//    {
//        IsPause = true;
//    }

//    public GameTimers UpdateAsFinish(float delta, Action onFinish = null)
//    {
//        if (!IsFinished && UnityEngine.Time.timeScale != 0)
//        {
//            _time += delta;
//            if (onFinish != _onFinish)
//            {
//                _onFinish = onFinish;
//            }
//            if (IsFinished)
//            {
//                _onFinish?.Invoke();
//            }
//        }
//        return this;
//    }

//    public void UpdateAsRepeat(float delta, Action onRepeat = null)
//    {
//        if (delta > _maxTime)
//        {
//            throw new Exception($"_maxTime too small, delta:{delta} > _maxTime:{_maxTime}");
//        }
//        _time += delta;
//        if (onRepeat != _onFinish)
//        {
//            _onFinish = onRepeat;
//        }
//        while (_time >= _maxTime)
//        {
//            _time -= _maxTime;
//            _onFinish?.Invoke();
//        }
//    }

//    public void OnFinish(Action onFinish)
//    {
//        _onFinish = onFinish;
//    }

//    public void OnRepeat(Action onRepeat)
//    {
//        _onFinish = onRepeat;
//    }
//}